<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class GoodsController extends Controller
{	
	//显示商品列表
	public function index(){
		//关联查询
		$result=DB::table('goods') 
		->join('uclassify','goods.cid','=','uclassify.cid')
		->Paginate(5);
		//引入模板分配数据
		return view('goods.index',['goods'=>$result]);
	}
	//显示商品添加模板
	public function add(){
		//查询所有分类
		$cates=DB::table('uclassify')
		->select(DB::raw('cid,cname,level,concat(path,"-",cid) as aspath'))
		->orderBy('aspath')->get();
		//引入模板分配数据
		return view('goods.add',['cates'=> $cates]);
	}
	//商品添加操作
	public function insert(Request $request){
		//接受表单传过来的值
		$goodsInfo=$request->only(['gname','gmoney','grepertory','gsay','gphoto','cid']);
		
		//上传图片
		if($request->hasFile('gphoto')){
			//文件路径
			$dir='uploads/';
			//获取后缀名
			$result=$request->file('gphoto')->getclientOriginalExtension();
			//上传文件名
			$fname=uniqid().'.'.$result;
			//dd($fname);
			//设置文件路径
			$goodsInfo['gphoto']=$dir.$fname;
			//dd($goodsInfo);
			//移动图片
			$request->file('gphoto')->move($dir,$fname);
		}else{
			//上传失败继续回添加页面
			//跳转商品添加页面
			return redirect('/goods/add');
		}
		$result=DB::table('goods')->insert($goodsInfo);
		//判断是否插入成功
		if($result){
			//跳转到商品页面
			return redirect('/goods/index');
		}else{
			//跳转到商品添加页面
			return redirect('/goods/add');
		}
		
	}
	
	//修改商品
	public function update($gid,$cid){
		//根据商品id查询详情
		$goods=DB::table('uclassify')->where('cid','=',$cid)->first();
		//dd($goods->cname);
		$goodsInfo=DB::table('goods')->where('gid','=',$gid)->first();
		$goodsInfo->cname=$goods->cname;
		//将获取的数据分配到模板显示
		return view('goods.update',['goods'=>$goodsInfo]);
	}
	
	//修改之后
	public function updateInfo(Request $request){
		//接受post表单提交的值
		$goodsInfo=$request->only(['gid','gmoney','grepertory','gsay','gphoto','cid']);
		$goodsInfo1=$request->only(['cname']);
		//上传图片
		//dd($goodsInfo['gphoto']);
		//$goodsInfo['gphoto']='uploads/'.$goodsInfo['gphoto'];
		//插入数据到数据库
		$result=DB::table('goods')->where('gid','=',$goodsInfo['gid'])->update($goodsInfo);
		
		$result2=DB::table('uclassify')->where('cid','=',$goodsInfo['cid'])->update($goodsInfo1);
		//判断是否插入成功
		if($result2 || $result ){
			//跳转到商品列表页面
			return redirect('/goods/index');
		}else{//失败
			//跳转到修改页面
			return redirect("/goods/update/{$goodsInfo['gid']}/{$goodsInfo['cid']}");
		}
	}
	
	//删除商品
	public function delete($gid){
		//删除商品
		$result=DB::table('goods')
		->join('shop','shop.gid','=','goods.gid')
		->where('gid','=',$gid)
		->first();
		$result1=DB::table('shop')->where('gid','=',$result->gid)->delete();
		$result2=DB::table('goods')->where('gid','=',$result->gid)->delete();
		//返回数据
		if($result){
			return redirect('/goods/index');
		}
	}
	//查询商品
	public function select(Request $request){
		//接受表单传过来的值
		$result=$request->only('name');	
		//查询结果
		$result2=DB::table('goods')
		->join('uclassify','goods.cid','=','uclassify.cid')
		->where('gname','like','%'.$result['name'].'%')
		->Paginate(3);
		return view('/goods/index',['goods'=>$result2]);
	}
}
